class ChangeNum:
    def __init__(self,num):
        self.__num = num

    """
    十六进制到十进制
    """
    def hexToDecimal(self):
        decimalValue = 0
        for ch in self.__num:
            if 'A' <= ch <= 'F' or '0' <= ch <= '9':
                decimalValue = decimalValue * 16 + self.__hexCharToDecimal(ch)
            else:
                return None
        return decimalValue

    def __hexCharToDecimal(self,ch):
        if 'A' <= ch <= 'F':
            return 10 + ord(ch) - ord('A')
        else:
            return ord(ch) - ord('0')
    """
    二进制转换十进制
    """
    def binaryToDecimal(self,binaryString):
        num = 0
        l = len(binaryString)
        for c in binaryString:
            num += eval(c) * (2 ** (l - 1))
            l -= 1
        return num

    def binaryToDecimal_2(self,binaryString):
        value = ord(binaryString[0]) - ord('0')
        for i in range(1, len(binaryString)):
            value = value * 2 + ord(binaryString[i]) - ord('0')
        return value;

def main():
    hex = input("请输入一个数字：").strip()

    changeNum = ChangeNum(hex.upper())
    if changeNum.hexToDecimal() == None:
        print("错误的数字")
    else:
        print("这个的数字所对应的十进制数为", changeNum.hexToDecimal())

    binary = input("请输入一个二进制字符串：")
    print(changeNum.binaryToDecimal(binary))
    print(changeNum.binaryToDecimal_2(binary))




main()
